<?php
namespace Weisite\Controller;
use Think\Controller;

class IndexController extends Controller{
    
    function _initialize(){
        $staticPath = __ROOT__ . '/Application/' . MODULE_NAME . '/Static';
        $sysConfig = array('TMPL_PARSE_STRING'  =>array(
        '__JS__'     => $staticPath . '/js',
        '__CSS__'     => $staticPath . '/css',
        '__STATIC__'     => $staticPath,
        ));
        
        C($sysConfig);
        
        $config = $this->getWeisiteConfig();
        
        //设置默认模板
        $default = array(
            'template_lists' => 'V1',
            'template_index' => 'ColorV1',
            'template_detail' => 'V1',
            'template_footer' => 'V2',
        );
        foreach ($default as $k => $v) {
            if(!isset($config[$k]) || !$config[$k]){
                $config[$k] = $v;
            }
        }

        define('CUSTOM_TEMPLATE_PATH', __ROOT__ . '/Application//' . MODULE_NAME . '/View/default/Template');
        $this->assign('config', $config);
    }

    // 首页
    function Index() {

        $map['is_show'] = 1;

        // 幻灯片
        $slideshow = D('weisiteSlideshow')->where($map)->order('sort asc, id desc')->select();

        foreach($slideshow as &$vo) {
            $vo['img'] = get_cover($vo['img'], 'path');
        }
        $this->assign('slideshow', $slideshow);
        // dump($slideshow);

        // 分类
        $category = S('weixsite_category_cache');
        if(!$category){
            $map = array(
                'is_show' => 1,
                'pid' => 0,
            );
            $category = D('Weisite/WeisiteCategory')->where($map)->order('sort asc, id desc')->select();
            foreach($category as &$vo) {
                $vo['icon'] = get_cover($vo['icon'], 'path');
                if(!$vo['url']){
                    $vo['url'] = U('lists', array('cate_id' => $vo['id']));
                }
            }
            if($category){
                S('weixsite_category_cache', $category);
            }
        }

        $this->assign('category', $category);

        $this->_footer();
        $this->display('TemplateIndex/' . $this->config['template_index'] . '/index');
    }
    // 分类列表
    function lists($page=1,$r=10) {
        $cid = I('get.cid', null, 'intval');
        $map = array();
        if($cid) {
            $map['cid'] = $cid;
        }
        $map['is_news'] = 1;
        
        $data = $this->getAreplyModel()->where($map)->order('id DESC')->page($page, $r)->select();

        /* 查询记录总数 */
        $count = $this->getAreplyModel()->where($map)->count();
        $list_data['list_data'] = $data;
        // 分页
        if($count > $row) {
            $page = new \Think\Page($count, $row);
            $page->setConfig('theme', '%FIRST% %UP_PAGE% %LINK_PAGE% %DOWN_PAGE% %END% %HEADER%');
            $list_data['_page'] = $page->show();
        }
        $this->assign($list_data);

        $this->_footer();
        $this->display('TemplateLists/' . $this->config['template_lists'] . '/lists');
    }
    
    protected function getAreplyModel(){
        return D('Weisite/WeisiteNews');
    }
    
    // 详情
    function detail() {
        $map['id'] = I('get.id', 0, 'intval');
        $info = $this->getAreplyModel()->where($map)->find();
        $this->assign('info', $info);
        $this->assign('page_title', $info['title']);

        M('custom_reply_news')->where($map)->setInc('view_count');

        $this->_footer();
        $this->display('TemplateDetail/V1/detail');

    }

    // 3G页面底部导航
    function _footer() {
//        $list = D('Addons://WeiSite/Footer')->get_list();

        $menu = S('weixsite_menu_cache');
        if(!$menu){
            $map = array(
//                'is_show' => 1,
                'is_menu' => 1,
            );
            $list = D('Weisite/WeisiteCategory')->where($map)->select();
            // 取一级菜单
            foreach($list as $k => $vo) {
                if($vo['pid'] != 0)
                    continue;

                if(!$vo['url']){
                    $vo['url'] = U('lists', array('cate_id' => $vo['id']));
                }
                if($vo['icon']){
                    $vo['icon'] = get_cover($vo['icon'], 'path');
                    $vo['icon'] = "<img src='{$vo['icon']}' title='{$vo['title']}' />";
                }
                $one_arr[$vo['id']] = $vo;
                unset($list[$k]);
                
            }

            foreach($one_arr as &$p) {
                $two_arr = array();
                foreach($list as $key => $l) {
                    if($l['pid'] != $p['id'])
                        continue;

                    if(!$l['url']){
                        $l['url'] = U('lists', array('cate_id' => $l['id']));
                    }
                    $two_arr[] = $l;
                    unset($list[$key]);
                }

                $p['child'] = $two_arr;
            }
            $menu = $one_arr;
            S('weixsite_menu_cache', $menu);
        }
        
        $total = count($menu);
        $width = 100;
        if($total){
            $width = 95 / $total;
            $width = floor($width);
        }
        $this->assign('nav_width', $width);
        $this->assign('footer', $menu);

        $html = $this->fetch('TemplateFooter/V2/footer');
        $this->assign('footer_html', $html);
    }
    
    protected $config = array();
    protected function getWeisiteConfig(){
        if($this->config){
            return $this->config;
        }
        $data = S('weixsite_config_cache');
        if(true || !$data){
            $configs = D('Config')->where(array('name' => array('like', '_' . strtoupper(MODULE_NAME) . '_' . '%')))->limit(999)->select();
            $data = array();
            foreach ($configs as $k => $v) {
                
                $key = str_replace('_' . strtoupper(MODULE_NAME) . '_', '', strtoupper($v['name']));
                $key = strtolower($key);
                $data[$key] = $v['value'];
            }
        }
        $this->config = $data;
        return $data;
    }

}